package com.rongxun.common.base;

import android.widget.Toast;

import com.rongxun.api.dao.ResponseDTO;
import com.rongxun.api.utils.FlyLog;
import com.rongxun.common.R;
import com.rongxun.common.base.mvp.IBase;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;

import io.reactivex.subscribers.ResourceSubscriber;
import retrofit2.HttpException;

public abstract class ResponseSubscriber<T> extends ResourceSubscriber<ResponseDTO<T>> {
    private final int SUCESS    = 200;//请求成功
    private final int FATAL_EOR = 500;  //返回数据失败,严重的错误

    private IBase.IView mView;


    public abstract void onSuccess(T t);

    public ResponseSubscriber(IBase.IView pView) {
        mView = pView;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mView != null) {
            mView.showLoading(true);
        }

    }

    @Override
    public void onNext(ResponseDTO<T> response) {

        if (response.getCode() == SUCESS) {
            onSuccess(response.getData());
        } else {
            onFailure(response.getCode(), response.getMessage());
        }
    }

    @Override
    public final void onError(Throwable t) {
        if (mView != null) {
            mView.showLoading(false);
        }
        int errorCode = -1111;

        mView.getContext().getString(R.string.httpexception);

        String errorMsg = mView.getContext().getString(R.string.unknownexception);
        if (t instanceof HttpException) {
            HttpException httpException = (HttpException) t;
            errorCode = httpException.code();
            errorMsg = mView.getContext().getString(R.string.httpexception);
        } else if (t instanceof SocketTimeoutException) {  //VPN open
            errorCode = FATAL_EOR;
            errorMsg = mView.getContext().getString(R.string.sockettimeoutexception);
        } else if (t instanceof ConnectException) {
            errorCode = FATAL_EOR;
            errorMsg = mView.getContext().getString(R.string.connectexception);
        } else if (t instanceof UnknownHostException) {
        } else if (t instanceof RuntimeException) {
            errorCode = FATAL_EOR;
            errorMsg = mView.getContext().getString(R.string.runtimeexception);
        } else if (t instanceof UnknownHostException) {
            errorCode = FATAL_EOR;
            errorMsg = mView.getContext().getString(R.string.unknownhostexception);
        } else if (t instanceof UnknownServiceException) {
            errorCode = FATAL_EOR;
            errorMsg = mView.getContext().getString(R.string.unknownserviceexception);
        } else if (t instanceof IOException) {  //飞行模式等
            errorCode = FATAL_EOR;
            errorMsg = mView.getContext().getString(R.string.ioexception);
        }
        FlyLog.e("errorCode" + errorCode + "errorMsg" + errorMsg);
        onFailure(errorCode, errorMsg);
    }


    /**
     * 简单的把Dialog 处理掉
     */
    @Override
    public void onComplete() {
        mView.showLoading(false);
        FlyLog.d("onComplete");
    }


    /**
     * Default error dispose!
     * 一般的就是 AlertDialog 或 SnackBar
     *
     * @param code
     * @param message
     */
    private void onFailure(int code, String message) {
        disposeEorCode(message, code);
        switch (code) {

        }

        Toast.makeText(mView.getContext(), message, Toast.LENGTH_SHORT).show();

    }

    /**
     * 对通用问题的统一拦截处理
     *
     * @param code
     */
    public abstract void disposeEorCode(String message, int code);


}
